package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseAttrValue;
import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import com.atguigu.gmall.product.mapper.BaseAttrValueMapper;
import com.atguigu.gmall.product.service.BaseAttrService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BaseAttrServiceImpl implements BaseAttrService {

    @Autowired
    BaseAttrInfoMapper baseAttrInfoMapper;

    @Autowired
    BaseAttrValueMapper baseAttrValueMapper;

    @Override
    public List<BaseAttrInfo> getAttrInfoListByCategoryId(Long category1Id, Long category2Id, Long category3Id) {
        return baseAttrInfoMapper.getAttrInfoListByCategoryId(category1Id,category2Id,category3Id);
    }

    /**
     * 添加修改二合一
     * @param baseAttrInfo
     */
    @Override
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        //先判断是新增还是修改
        Long infoId = baseAttrInfo.getId();
        //如果id为空,则为新增平台属性
        if (infoId == null){
            addBaseAttrInfo(baseAttrInfo);
        }else {
            updateBaseAttrInfo(baseAttrInfo);
        }
    }

    /**
     * 属性修改
     *
     * @param baseAttrInfo
     */
    private void updateBaseAttrInfo(BaseAttrInfo baseAttrInfo) {
        //1.修改数据库中,属性名信息
        baseAttrInfoMapper.updateById(baseAttrInfo);

        //2.修改所有的属性值信息,
        // bug:只遍历了前端带来的数据,前端没带的其实要删除
        List<BaseAttrValue> list = baseAttrInfo.getAttrValueList();
        QueryWrapper<BaseAttrValue> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_id",baseAttrInfo.getId());
        //先删除原来所有的属性值
        baseAttrValueMapper.delete(wrapper);
        for (BaseAttrValue value : list) {
            //都是新增
            if (value.getId() == null){
                //新增属性值,回填attr_id
                value.setAttrId(baseAttrInfo.getId());
                baseAttrValueMapper.insert(value);
            }else{
                //修改属性值
                baseAttrValueMapper.insertWithId(value);
            }
        }
    }

    /**
     * 属性新增
     * @param baseAttrInfo
     */
    private void addBaseAttrInfo(BaseAttrInfo baseAttrInfo){
        //1.先保存base_attr_info相关信息
        baseAttrInfoMapper.insert(baseAttrInfo);
        //获取到刚才保存成功的自增id
        Long id = baseAttrInfo.getId();

        //2.再保存base_attr_value相关信息
        List<BaseAttrValue> list = baseAttrInfo.getAttrValueList();
        for (BaseAttrValue value : list) {
            //回填属性id
            value.setAttrId(id);
            //保存属性值
            baseAttrValueMapper.insert(value);
        }
    }

    /**
     * 按照平台属性的id,获取平台属性的所有属性值
     * @param attrId
     * @return
     */
    @Override
    public List<BaseAttrValue> getAttrValueList(Long attrId) {
        //select * from base_attr_value where attr_id=#{attrId}
        QueryWrapper<BaseAttrValue> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_id",attrId);
        List<BaseAttrValue> values = baseAttrValueMapper.selectList(wrapper);
        return values;
    }

    /*@Override
    public List<BaseAttrInfo> getAttrInfoListByCategoryId(Long category1Id, Long category2Id, Long category3Id) {

        QueryWrapper<BaseAttrInfo> wrapper = new QueryWrapper<>();

        if (category1Id != 0) {
            //一级分类不为null
            wrapper.eq("category_id",category1Id);
            wrapper.eq("category_level",1);
        }

        if (category2Id != 0){
            //二级分类不为null
            QueryWrapper<BaseAttrInfo> or = wrapper.or();
            or.eq("category_id",category2Id);
            or.eq("category_level",2);
        }

        if (category3Id != 0){
            //三级分类不为空
            QueryWrapper<BaseAttrInfo> or = wrapper.or();
            or.eq("category_id",category3Id);
            or.eq("category_level",3);

        }

        //select * from base_attr_info
        List<BaseAttrInfo> attrInfos = baseAttrInfoMapper.selectList(wrapper);

        return attrInfos;
    }*/
}
